package cn.iocoder.yudao.module.pickbook.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.voicerecording.PickbookUserVoiceRecordingDO;
import cn.iocoder.yudao.module.pickbook.service.voicerecording.PickbookUserVoiceRecordingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "配音 APP API")
@RestController
@RequestMapping("/pickbook/app/voice-recording")
@Validated
@Slf4j
public class PickbookAppVoiceRecordingController {

    @Resource
    private PickbookUserVoiceRecordingService voiceRecordingService;

    @PostMapping("/create")
    @Operation(summary = "创建配音记录")
    public CommonResult<PickbookUserVoiceRecordingDO> createVoiceRecording(
            @RequestParam @NotNull(message = "用户ID不能为空") Long userId,
            @RequestParam @NotNull(message = "绘本ID不能为空") Long bookId,
            @RequestParam(required = false) Long chapterId,
            @RequestParam @NotNull(message = "音频ID不能为空") Long audioId,
            @RequestParam @NotBlank(message = "配音标题不能为空") String title,
            @RequestParam(required = false) String description) {
        
        PickbookUserVoiceRecordingDO recording = voiceRecordingService.createVoiceRecording(
                userId, bookId, chapterId, audioId, title, description);
        return success(recording);
    }

    @GetMapping("/list")
    @Operation(summary = "获取用户配音记录列表")
    public CommonResult<PageResult<PickbookUserVoiceRecordingDO>> getVoiceRecordingList(
            @RequestParam @NotNull(message = "用户ID不能为空") Long userId,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        
        PageResult<PickbookUserVoiceRecordingDO> result = voiceRecordingService.getUserVoiceRecordings(
                userId, pageNo, pageSize);
        return success(result);
    }

    @GetMapping("/detail")
    @Operation(summary = "获取配音记录详情")
    public CommonResult<PickbookUserVoiceRecordingDO> getVoiceRecordingDetail(
            @RequestParam @NotNull(message = "配音记录ID不能为空") Long id) {
        
        PickbookUserVoiceRecordingDO recording = voiceRecordingService.getVoiceRecording(id);
        return success(recording);
    }

    @PutMapping("/score")
    @Operation(summary = "更新配音评分")
    public CommonResult<Boolean> updateVoiceRecordingScore(
            @RequestParam @NotNull(message = "配音记录ID不能为空") Long id,
            @RequestParam(required = false) Integer score) {
        
        voiceRecordingService.updateVoiceRecordingScore(id, score);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除配音记录")
    public CommonResult<Boolean> deleteVoiceRecording(
            @RequestParam @NotNull(message = "配音记录ID不能为空") Long id) {
        
        voiceRecordingService.deleteVoiceRecording(id);
        return success(true);
    }

    @PostMapping("/generate-score")
    @Operation(summary = "自动生成配音评分")
    public CommonResult<Integer> generateVoiceRecordingScore(
            @RequestParam @NotNull(message = "配音记录ID不能为空") Long id) {
        
        // 调用Service的自动评分功能
        voiceRecordingService.updateVoiceRecordingScore(id, null);
        
        // 获取更新后的配音记录
        PickbookUserVoiceRecordingDO recording = voiceRecordingService.getVoiceRecording(id);
        return success(recording.getScore());
    }

} 